package com.itcast.demo_one.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.graphics.toColorInt
import androidx.recyclerview.widget.RecyclerView
import com.itcast.demo_one.R

class ColorAdapter: RecyclerView.Adapter<ColorAdapter.PageViewHolder>() {
    // 数据类
    data class PageData(val color: String, val text: String)

    // 页面数据：颜色 + 文本
    private val pages = listOf(
        PageData("#FF5733", "首页"),
        PageData("#33FF57", "分类"),
        PageData("#3357FF", "我的")
    )

    inner class PageViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
          val textView: TextView = itemView.findViewById(R.id.pageText)
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): PageViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_page, parent, false)
        return PageViewHolder(view)
    }

    override fun onBindViewHolder(
        holder: PageViewHolder,
        position: Int
    ) {
        val page = pages[position]
        with(holder) {
            textView.text = page.text
            itemView.setBackgroundColor(page.color.toColorInt())
        }
    }

    override fun getItemCount(): Int {
        return pages.size
    }
}